//
//  LOLFirstPager.m
//  LOL_Little_Box
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LOLFirstPager.h"
#import "LOLNavigationController.h"
#define ABC [UIColor colorWithRed:(arc4random() % ((255 - 1 + 1) + 1) / 255.0) green:(arc4random() % ((255 - 1 + 1) + 1) / 255.0) blue:(arc4random() % ((255 - 1 + 1) + 1) / 255.0) alpha:1]
@interface LOLFirstPager ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, retain)UIScrollView *titleScrollView;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIScrollView *imageScrollView;
@property (nonatomic, retain)NSMutableArray *imageArray;
@property (nonatomic, retain)NSArray *array;
@property (nonatomic, retain)UITableView *tableView;
@end

@implementation LOLFirstPager

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"nav_bar_bg_for_six@2x"]];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 32 - 64)];
    [self.view addSubview:self.scrollView];
    self.scrollView.backgroundColor = ABC;
    self.scrollView.contentSize = CGSizeMake(WIDTH * 8, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    
    self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 5)];
    self.imageScrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.imageScrollView.pagingEnabled = YES;
    self.imageScrollView.showsHorizontalScrollIndicator = NO;
    self.imageScrollView.delegate = self;
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    [self.scrollView addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.backgroundColor = ABC;
    self.tableView.tableHeaderView = self.imageScrollView;
    [self creatSubScr];
    
    
}

- (void)creatSubScr{
    self.titleScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 24, WIDTH, 40)];
    self.navigationItem.titleView = self.titleScrollView;
    self.array = [NSArray arrayWithObjects:@"最新", @"赛事", @"视频", @"娱乐", @"官方", @"美女", @"攻略", @"活动", nil];
    for (NSInteger i = 0; i < _array.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:_array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        button.frame = CGRectMake(50 * i, 10, 40, 30);
        [self.titleScrollView addSubview:button];
        button.tag = 10000 + i;
        if (button.tag != 10000) {
            button.alpha = 0.5;
        }
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.titleScrollView.contentSize = CGSizeMake(50 * _array.count, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        if (scrollView.contentOffset.x == WIDTH * self.array.count) {
            scrollView.contentOffset = CGPointMake(0, 0);
        } else if (scrollView.contentOffset.x == 0) {
            scrollView.contentOffset = CGPointMake(WIDTH * self.array.count, 0);
        }
    }
    if (scrollView == self.scrollView) {
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        animation.duration = 0.2;
        animation.values = @[@(1.3)];
        CGFloat i = scrollView.contentOffset.x / WIDTH;
        UIButton *button = (UIButton *)[self.titleScrollView viewWithTag:10000 + i];
        [button.layer addAnimation:animation forKey:@"scale"];
        button.alpha = 1;
        for (UIButton *temp in self.titleScrollView.subviews) {
            if (temp != button) {
                temp.alpha = 0.5;
            }
        }
        if (scrollView.contentOffset.x == 0) {
            self.titleScrollView.contentOffset = CGPointMake(0, 0);
        }
    }
}

- (void)click:(UIButton *)button {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.2;
    animation.values = @[@(1.3)];
    [button.layer addAnimation:animation forKey:@"scale"];
    button.alpha = 1;
    for (UIButton *temp in self.titleScrollView.subviews) {
        if (temp != button) {
            temp.alpha = 0.5;
        }
    }
    self.scrollView.contentOffset = CGPointMake((button.tag - 10000) * WIDTH, 0);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 10;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any self.navigationController.navigationBar.hidden = YES; that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
